package student.action;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.PreparedStatement;
import java.util.ArrayList;
import java.util.Enumeration;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;


import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.util.DB;
import com.util.MyLog;

import edu.student.models.Lookup;
import edu.student.models.Subject;


public class AdminSyllabus {
	private String CourseName;
	private int ChapterNo;
	private String ChapterName;
	private String msg;
	private String commandButton;
	int subject_id;
	private ArrayList<Subject> subjects;
	public String getCourseName() {
		return CourseName;
	}
	public void setCourseName(String courseName) {
		CourseName = courseName;
	}
	public int getChapterNo() {
		return ChapterNo;
	}
	public void setChapterNo(int chapterNo) {
		ChapterNo = chapterNo;
	}
	public String getChapterName() {
		return ChapterName;
	}
	public void setChapterName(String chapterName) {
		ChapterName = chapterName;
	}
	public String getMsg() {
		return msg;
	}
	public void setMsg(String msg) {
		this.msg = msg;
	}
	
public String execute() throws Exception {
	HttpServletRequest request=(HttpServletRequest)ActionContext.getContext().get(ServletActionContext.HTTP_REQUEST);
	Lookup subject=Lookup.findOne("subject", "where subject_name = '"+CourseName+"'");
	System.out.println("In execute() value of coursename is"+ CourseName);
	subject_id=subject.getId();
	 commandButton=request.getParameter("commandButton");
	System.out.println("commandButton"+ commandButton);
	if (commandButton.startsWith("Add")) {
		int flag=ifExist(subject_id);
		if(flag == 0){
			msg="The chapter " +ChapterName +" has already been added. Please add some other chapter";
		}
		else {
    	int result=insert();
  		 if(result>0){
  			 msg="Added Syllabus for Course "+ CourseName +"!!";
  			 
  		 }
  		 else{
  			msg="Cannot Add Syllabus for course "+CourseName ;
  			 //MyLog.log("Cannot Add, course "+subject_name +" already added to the semester "+semester_name);
  			 return "displayMessage";
  		 }
  		
		}
	 	

  		return "displayMessage";
	}
	else if (this.commandButton.startsWith("Back to Admin")) {
			return "admin_home";
		}
	else if (this.commandButton.startsWith("View")) {
		setSubjects(Subject.findAllSubjectsInDatabase());
		
		return "viewSyllabus";
	}
	return "error";
}
public int insert() {
    System.out.println("value of subjectid,chap no, chap name is" + subject_id + "" + ChapterNo + "" + ChapterName);
	String insertSQL = "insert into syllabus(subject_id,chapter_no,chapter_name) values" +
							"("+subject_id +", "+ChapterNo+ ",'"+ChapterName+ "');";
	MyLog.log("insert query"+insertSQL);
	return DB.update(insertSQL);
}
		public int ifExist(int subject_id){
			//String sql= "select * from syllabus where subject_id ="+subject_id + "and chapter_name ='"+ChapterName +"';";
			String sql="select * from syllabus where subject_id = ? and chapter_name = ?";
			int flag=1;
			System.out.println("subjectId="+ subject_id + "Chaptername is "+ ChapterName);
			Connection con = DB.getConnection();
			try {
				
				PreparedStatement preparedStatement = con.prepareStatement(sql);
				preparedStatement.setInt(1,subject_id);
				preparedStatement.setString(2,ChapterName);
				
				
				ResultSet rs = preparedStatement.executeQuery();
				System.out.println("here in ifexist");
				if(rs.next())
					flag=0;
								

			} catch (SQLException e) {
				MyLog.log("Error while checking syllabus"+e);
			}
			System.out.println("flag="+ flag);
			DB.close(con);
			return flag;
		}
		
		public ArrayList<Subject> getSubjects() {
			return subjects;
		}
		public void setSubjects(ArrayList<Subject> subjects) {
			this.subjects = subjects;
		}
		
}
